import { Controller, Post, Body, UseGuards, Req } from '@nestjs/common/decorators'
import { CommentService } from './comment.service'
import { CreateCommentDto } from './dto/create-comment.dto'
import { ListCommentDto } from './dto/list-comment.dto'
import { ApiBearerAuth, ApiOperation, ApiTags, ApiConsumes } from '@nestjs/swagger'
import { TokenService } from '@/src/auth/token.service'
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'
import { Request } from 'express'
import { RateLimit } from '../common/rateLimit/rate-limit.decorator'

@ApiTags('user')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth('JWT')
@Controller('user/comment')
export class CommentController {
  constructor(
    private readonly commentService: CommentService,
    private readonly tokenService: TokenService,
  ) { }

  // 评论列表
  @ApiOperation({ summary: '留言list' })
  @ApiConsumes('application/x-www-form-urlencoded')
  @RateLimit(30, 60 * 1000)
  @Post('list')
  async list(@Body() query: ListCommentDto) {
    const body = await this.commentService.list(query)
    return body
  }

  // 添加评论
  @ApiOperation({ summary: '添加留言' })
  @ApiConsumes('application/x-www-form-urlencoded')
  @RateLimit(10, 5 * 60 * 1000)
  @Post('save')
  async save(@Body() data: CreateCommentDto, @Req() req: Request) {
    await this.commentService.create({ ...data, user_id: req.user?.id as number })
    return {
      isAdd: true
    }
  }
}
